package kafka;

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.KafkaAdminClient;
import org.apache.kafka.clients.producer.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author 颜培轩
 * @date 2018年08月09日  上午 09:52
 */

public class Productor {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        CountDownLatch countDownLatch = new CountDownLatch(100);
        /*Map<String, String> map = new HashMap<>();
        map.put("hello", "world");
        map.put("hello1", "world");
        map.put("hello2", "world");
        map.put("hello3", "world");
        map.put("hello4", "world");
        map.put("hello5", "world");
        map.put("hello6", "world");
        map.put("hello7", "world");
        map.put("hello8", "world");
        map.put("hello9", "world");
        map.put("hello10", "world");
        map.put("hello11", "world");
        map.put("hello12", "world");
        map.put("hello13", "world");
        map.put("hello14", "world");
        map.put("hello15", "world");*/

        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        props.put(ProducerConfig.RETRIES_CONFIG, 0);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<String, String>("ag01", Integer.toString(i), Integer.toString(i)), new Callback() {
                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    System.out.println("metadata = " + metadata);
                }
            });
        }
        producer.close();
    }
}
